package com.learning.jquizenglish.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.learning.jquizenglish.common.service.AudioStreamService;
import com.learning.jquizenglish.player.MusicService;
import com.learning.jquizenglish.player.MusicService.State;

/**
 * The Class BaseAudioPlayerActivity.
 */
public class BaseAudioPlayerActivity extends BaseFragmentActivity{

	/** The m play pause button. */
	protected Button mPlayPauseButton;
	
	/** The m audio url. */
	protected String mAudioUrl;
	
	/** The m buffer broadcast is registered. */
	boolean mBufferBroadcastIsRegistered;
	
	/** The current state. */
	private State currentState;
	
	/* (non-Javadoc)
	 * @see com.learning.jquizenglish.activity.BaseFragmentActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
	}
	
	/**
	 * Sets the button controller.
	 *
	 * @param btnPlayPause the new button controller
	 */
	public void setButtonController(Button btnPlayPause){
		mPlayPauseButton = btnPlayPause;
		mPlayPauseButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (currentState == State.Paused) {
					playAudio();
					mPlayPauseButton.setText("Pause");
					currentState = State.Playing;
				}else if (currentState == State.Playing) {
					pauseAudio();
					mPlayPauseButton.setText("Play");
					currentState = State.Paused;
				} else if (currentState == State.Stopped) {
					replay();
				}
			}
		});
	}
	
	/**
	 * Play audio.
	 */
	public void playAudio(){
    	startService(new Intent(MusicService.ACTION_PLAY));
    }
    
    /**
     * Pause audio.
     */
    public void pauseAudio(){
    	startService(new Intent(MusicService.ACTION_PAUSE));
    }
    
    /**
     * Skip audio.
     */
    public void skipAudio(){
    	startService(new Intent(MusicService.ACTION_SKIP));
    }
    
    /**
     * Rewind audio.
     */
    public void rewindAudio(){
    	startService(new Intent(MusicService.ACTION_REWIND));
    }
    
    /**
     * Stop audio.
     */
    public void stopAudio(){
    	startService(new Intent(MusicService.ACTION_STOP));
    }
    
    /**
     * Sets the audio url.
     *
     * @param url the new audio url
     */
    public void setAudioUrl(String url){
    	mAudioUrl = url;
    	// MusicService.
        Intent i = new Intent(MusicService.ACTION_URL);
        Uri uri = Uri.parse(mAudioUrl);
        i.setData(uri);
        startService(i);
    }
    
    public void replay(){
    	// MusicService.
        Intent i = new Intent(MusicService.ACTION_URL);
        Uri uri = Uri.parse(mAudioUrl);
        i.setData(uri);
        startService(i);
    }
	
 // -- onPause, unregister broadcast receiver. To improve, also save screen
 	// data ---
 	/* (non-Javadoc)
  * @see android.support.v4.app.FragmentActivity#onPause()
  */
 @Override
 	protected void onPause() {
 		// Unregister broadcast receiver
 		if (mBufferBroadcastIsRegistered) {
 			unregisterReceiver(broadcastMediaStateReceive);
 			mBufferBroadcastIsRegistered = false;
 		}
 		super.onPause();
 	}

 	// -- onResume register broadcast receiver. To improve, retrieve saved
 	// screen data ---
 	/* (non-Javadoc)
	  * @see android.support.v4.app.FragmentActivity#onResume()
	  */
	 @Override
 	protected void onResume() {
 		// Register broadcast receiver
 		if (!mBufferBroadcastIsRegistered) {
 			registerReceiver(broadcastMediaStateReceive, new IntentFilter(
 					MusicService.BROADCAST_MEDIA_STATE));
 			mBufferBroadcastIsRegistered = true;
 		}
 		if (currentState == State.Paused) {
			playAudio();
			mPlayPauseButton.setText("Pause");
			currentState = State.Playing;
		}
 		super.onResume();
 	}
    
	 @Override
	protected void onStop() {
		 if (currentState == State.Playing) {
				pauseAudio();
				mPlayPauseButton.setText("Play");
				currentState = State.Paused;
		 }
		super.onStop();
	}
	 
 	/**
	  * Config media.
	  *
	  * @param mediaStateIntent the media state intent
	  */
	 public void configMedia(Intent mediaStateIntent){
 		String state = mediaStateIntent.getStringExtra(MusicService.STATE_KEY);
 		if (state.equals(MusicService.STATE_PREPARING)) {
			if (mPlayPauseButton!=null) {
				mPlayPauseButton.setEnabled(false);
				mPlayPauseButton.setText("Audio is loading...");
				currentState = State.Preparing;
			}
		}else if (state.equals(MusicService.STATE_PLAYING)) {
			if (mPlayPauseButton!=null) {
				mPlayPauseButton.setEnabled(true);
				mPlayPauseButton.setText("Pause");
				currentState = State.Playing;
			}
		} else if (state.equals(MusicService.STATE_STOPPED)) {
			if (mPlayPauseButton!=null) {
				mPlayPauseButton.setEnabled(true);
				mPlayPauseButton.setText("Play");
				currentState = State.Stopped;
			}
		}
 	}
 // Set up broadcast receiver
 	/** The broadcast media state receive. */
 private BroadcastReceiver broadcastMediaStateReceive = new BroadcastReceiver() {
 		@Override
 		public void onReceive(Context context, Intent mediaStateIntent) {
 			configMedia(mediaStateIntent);
 		}
 	};
 	
 	public void setResultOk(){
		Intent returnIntent = new Intent();
		setResult(RESULT_OK,returnIntent);
	}
	
	public void setResultCancel(){
		Intent returnIntent = new Intent();
		setResult(RESULT_CANCELED,returnIntent);
	}
}
